package Client;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class ConnectClientToServer extends JFrame{
	
	private JTextField userName;
	private JTextField ipAddress;
	private JTextField port;
	private JButton connect;
	private JButton cancel;
	public static ConnectClientToServer instanta;
	
	public static ConnectClientToServer getConnectInstance(){
		if(instanta==null)
			instanta=new ConnectClientToServer();
		return instanta;
	}
	
	public ConnectClientToServer() {
		super("Connect to chat room");
		initConnectUser();
	}

	private void initConnectUser() {
		JPanel panelConnect = new JPanel();
		panelConnect.setBackground(new Color(0.3f,0.8f,0.9f));
		panelConnect.setLayout(null);
//		Image background = Toolkit.getDefaultToolkit().createImage("connect.jpg");
//		panelConnect.se(background, 0, 0, null);
		//userName
		Border blueborder = BorderFactory.createLineBorder(Color.blue);
		JLabel labelUserName = new JLabel("User Name:");
		labelUserName.setBounds(10, 10, 100, 25);
		panelConnect.add(labelUserName);
		userName = new JTextField();
		userName.setBorder(blueborder);
		userName.setBounds(100, 10, 100, 25);
		panelConnect.add(userName);
		//address
		JLabel labeladdressIp = new JLabel("IP Address:");
		labeladdressIp.setBounds(10, 40, 100, 25);
		panelConnect.add(labeladdressIp);
		ipAddress = new JTextField("127.0.0.1");
		ipAddress.setBorder(blueborder);
		ipAddress.setBounds(100, 40, 100, 25);
		panelConnect.add(ipAddress);
		//port
		JLabel labelPort = new JLabel("Port:");
		labelPort.setBounds(10, 70, 100, 25);
		panelConnect.add(labelPort);
		port = new JTextField("7665");
		port.setBorder(blueborder);
		port.setBounds(100, 70, 100, 25);
		panelConnect.add(port);
		//button connect
		connect = new JButton(new ImageIcon("Connect_button.png"));
		connect.setToolTipText("Connect");
		connect.setBounds(80, 140, 100, 25);
		connect.addActionListener(new Action());
		panelConnect.add(connect);
		//button cancel
		cancel = new JButton(new ImageIcon("Cancel_button.png"));
		cancel.setToolTipText("Cancel");
		cancel.setBounds(190, 140, 100, 25);
		cancel.addActionListener(new Action());
		panelConnect.add(cancel);
		
		this.add(panelConnect);
		this.setVisible(true);
		this.setResizable(false);
		this.setSize(300, 200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		instanta = this;
	}
	private void closeConnect(){
	 this.dispose();
	}
	class Action implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == cancel){
				userName.setText("");
				ipAddress.setText("");
				port.setText("");
			}
			if (e.getSource() == connect){
				if(getUserName().equals("")){
					JOptionPane.showMessageDialog(null, "Please enter a name");
					return;
				}if(getIpAddress().equals("")){
					JOptionPane.showMessageDialog(null, "Please enter IP address");
					return;
				}if(getPort().equals("")){
					JOptionPane.showMessageDialog(null, "Please enter port");
					return;
				}	
				closeConnect();
				new Client_Gui();
			}
			
		}
	}
	public String getUserName() {
		return userName.getText();
	}
	public String getIpAddress() {
		return ipAddress.getText();
	}
	public String getPort() {
		return port.getText();
	}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}
	
	public static void main(String args[]){
		new ConnectClientToServer();
	}
	
}